#!/bin/bash

function server_recv_agent() {
  echo_start "$1 ... ..."
  confirm_operate "depoly server_recv_agent"
  if [ $? -eq 0 ]; then
    if [ -f $HRS_TOMCAT_HOME/webapps/A/startAgent ]; then 
      kill_proc "HrdsReceiveAgent"
      echo_info "shutdown receiveAgent ... ... done."
    fi

    # 初始化主目录
    # 目前 HRS_RECEIVEAGENT_HOME 是错误的，没有用处。需要把A的receiveAgent整体打包并部署到该目录
    init_dir $HRS_RECEIVEAGENT_HOME
    # 初始化日志目录
    init_dir $HRS_RECEIVEAGENT_LOGS
    echo_info "init dir done."

    # 修改startAgent的日志目录
    sed -i "s%^log4j\.appender\.DailyFile\.File.*%log4j\.appender\.DailyFile\.File=$HRS_RECEIVEAGENT_LOGS/receiveAgent\.log%g" $HRS_TOMCAT_HOME/webapps/A/log4j.properties || { die "modify [$HRS_TOMCAT_HOME/webapps/A/log4j.properties] fail, installing abort"; }
    chown -R $HYREN_USER:$HYREN_USER $HRS_RECEIVEAGENT_LOGS
    chown -R $HYREN_USER:$HYREN_USER $HRS_RECEIVEAGENT_HOME
    echo_info "init property done."

    su - $HYREN_USER -c "cd $HRS_TOMCAT_HOME/webapps/A && nohup bash startAgent &"
    echo_info "start service done."

    # 设置环境变量
    #set_bash_profile "HRS TOMCAT ENV" "HRSCOMMENT" "$HYREN_USER"
    set_bash_profile "HRS_RECEIVEAGENT_HOME" "$HRS_RECEIVEAGENT_HOME" "$HYREN_USER"
    set_bash_profile "HRS_RECEIVEAGENT_LOGS" "$HRS_RECEIVEAGENT_LOGS" "$HYREN_USER"

    echo_info "****************************************"
    echo_info "RECVAGNT HOME   dir  : $HRS_RECEIVEAGENT_HOME"
    echo_info "RECVAGNT LOGS   dir  : $HRS_RECEIVEAGENT_LOGS"
    echo_info "****************************************"

    echo_passed "$1 done."
  else
    echo_passed "$1 SKIP"
  fi
}  

